/**
 * 
 */
package com.kexinde.wjifen.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.kexinde.wjifen.config.CommonCfg;
import com.kexinde.wjifen.dao.GiftDao;
import com.kexinde.wjifen.dao.GiftExchangeDao;
import com.kexinde.wjifen.model.Gift;
import com.kexinde.wjifen.vo.GiftDetail;
import com.kexinde.wjifen.vo.GiftDetailExchange;
import com.kexinde.wjifen.vo.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author vincent
 * 
 */
public class GiftAction extends ActionSupport implements RequestAware,
		SessionAware {

	private final static Logger logger = Logger.getLogger(GiftAction.class);
	private Map<String, Object> request;
	private Map<String, Object> session;
	private PageInfo pageInfo;
	private int listType = 0; // 查询类型，0为最热，1为积分，2为最新
	private Long giftId; // 要查询的商品id
	private GiftDetail exchangeGiftDetail; // 放入兑换车的商品类

	/**
	 * 微商城按钮
	 * 
	 * @return
	 */
	public String listAll() {
		try {
			if (pageInfo == null) {
				pageInfo = new PageInfo(1, CommonCfg.listLineSize); // 当前第一页，每页20条记录
			}

			GiftDao giftDao = new GiftDao();

			if (listType == 0) { // 按热门度查询商品
				List<Gift> allHotGifts = giftDao.queryAllHotGifts(pageInfo);
				request.put("allGifts", allHotGifts); // 把热门商品放进allGifts

			} else if (listType == 1) { // 按积分查询商品
				List<Gift> allPointGifts = giftDao.queryAllPointGifts(pageInfo);
				request.put("allGifts", allPointGifts); // 把积分商品放进allGifts

			} else if (listType == 2) { // 按创建时间查询商品
				List<Gift> allNewGifts = giftDao.queryAllNewGifts(pageInfo);
				request.put("allGifts", allNewGifts); // 把最新商品放进allGifts

			}

		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 显示指定id商品，跳转到单个商品页面
	 * 
	 * @return
	 */
	public String showGiftDetail() {
		try {
			this.giftId = (Long) request.get("giftId");
			if (this.giftId != null) {// giftId不为空
				GiftDao giftDao = new GiftDao();
				Gift gift = giftDao.findOneById(giftId);
				if (gift != null) { // gift读取成功
					// 取detail
					GiftDetail giftDetail = new GiftDetail();
					giftDetail.setId(gift.getId());
					giftDetail.setName(gift.getName());
					giftDetail.setPic(gift.getPic());
					giftDetail.setAmount(gift.getAmount());
					giftDetail.setPoint(gift.getPoint());
					giftDetail.setStocks(gift.getStocks());
					giftDetail.setProfile(gift.getProfile());
					// 下列属性暂未完善
					giftDetail.setFreight(0.0);// 运费
					giftDetail.setStyle("");// 款式

					// 取兑换记录集合（分页）
					if (pageInfo == null) {
						pageInfo = new PageInfo(1,
								CommonCfg.giftExchangeLineSize); // 当前第一页，每页10条记录
					}
					GiftExchangeDao giftExchangeDao = new GiftExchangeDao();
					List<GiftDetailExchange> giftDetailExchanges = giftExchangeDao
							.GiftDetailExchangeLogs(giftId, pageInfo);

					// 填充兑换记录中所缺属性
					for (GiftDetailExchange giftDetailExchange : giftDetailExchanges) {
						giftDetailExchange.setGiftName(giftDetail.getName());
						giftDetailExchange.setGiftId(giftDetail.getId());
						Long point = giftDetail.getPoint()
								* giftDetailExchange.getCount();
						giftDetailExchange.setPoint(point);
					}

					// 把giftDetail和兑换记录放入request
					request.put("giftDetail", giftDetail);
					request.put("giftDetailExchanges", giftDetailExchanges);
					return SUCCESS;

				} else {// gift读取失败
					request.put("feedbackMsg", "gift读取失败");
					return ERROR;
				}

			} else { // giftId为空，提示出错
				request.put("feedbackMsg", "giftId为空");
				return ERROR;
			}

		} catch (Exception ex) {
			request.put("feedbackMsg", ex);
			logger.error(ex);
			return ERROR;
		}
	}

	/**
	 * 将商品放入兑换车
	 * 
	 * @return
	 */
	public String addToShoppingList() {// 未完善，缺少检查商品是否下架
		try {
			if (session.get("user") != null) {	//已经登录
				if (exchangeGiftDetail != null) {// 不为空，继续操作
					if(exchangeGiftDetail.getNum()<=0){
						exchangeGiftDetail.setNum(1);	//若num小于等于0，设置为1
					}
					if (exchangeGiftDetail.getStocks() >= exchangeGiftDetail
							.getNum()) {// 判断库存是否足够
						List<GiftDetail> shoppingList = (List<GiftDetail>) session
								.get("shoppingList");
						if (shoppingList != null) { // 已有商品在购物车
							shoppingList.add(exchangeGiftDetail);
							session.put("shoppingList", shoppingList);
							request.put("feedbackMsg", "加入兑换车成功");
							request.put("giftId",exchangeGiftDetail.getId());
							return SUCCESS;

						} else { // 第一次将商品加入购物车
							shoppingList = new ArrayList<GiftDetail>();
							shoppingList.add(exchangeGiftDetail);
							session.put("shoppingList", shoppingList);
							request.put("feedbackMsg", "加入兑换车成功");
							request.put("giftId",exchangeGiftDetail.getId());
							return SUCCESS;
						}

					} else {// 库存不足
						request.put("feedbackMsg", "库存不足");
						return ERROR;
					}
				} else {
					request.put("feedbackMsg", "商品类为空");
					return ERROR;
				}

			} else { // 未登录
				request.put("feedbackMsg", "未登录");
				return ERROR;
			}

		} catch (Exception ex) {
			request.put("feedbackMsg", ex);
			logger.error(ex);
			return ERROR;
		}
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public int getListType() {
		return listType;
	}

	public void setListType(int listType) {
		this.listType = listType;
	}

	public Long getGiftId() {
		return giftId;
	}

	public void setGiftId(Long giftId) {
		this.giftId = giftId;
	}

	public GiftDetail getExchangeGiftDetail() {
		return exchangeGiftDetail;
	}

	public void setExchangeGiftDetail(GiftDetail exchangeGiftDetail) {
		this.exchangeGiftDetail = exchangeGiftDetail;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
